import 'package:flutter/material.dart';
import 'package:znmm/config/app_asset.dart';

class GTPBtnManager {
  static OverlayEntry? _overlayEntry;
  static bool _isVisible = false;

  static void showButton(BuildContext context) {
    _overlayEntry?.remove();
    _overlayEntry = null;
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _overlayEntry ??= OverlayEntry(
        builder: (BuildContext context) {
          return _isVisible
              ? Positioned(
                  bottom: 100.0,
                  right: 20.0,
                  child: IconButton(
                    onPressed: () {
                      //Get.toNamed(RouteNames.chatGptRoute);
                    },
                    icon: Container(
                      width: 30,
                      height: 30,
                      decoration: BoxDecoration(
                        //color: Colors.black,
                        image: const DecorationImage(
                            image: AssetImage(AppAsset.imageLogo),
                            fit: BoxFit.cover),
                        shape: BoxShape.circle,
                        boxShadow: [
                          BoxShadow(
                            color: Colors.black.withOpacity(0.6), // 阴影颜色
                            blurRadius: 10, // 模糊半径
                            spreadRadius: 2, // 阴影扩散程度
                            offset: const Offset(0, 3), // 阴影偏移量
                          ),
                        ],
                      ),
                    ),
                  )

                  // FloatingActionButton(
                  //   onPressed: () {
                  //     print('Button Pressed');
                  //     Get.toNamed(RoutePath.chatpage);
                  //   },
                  //   child: Image.asset(
                  //     'assets/images/gpt.png',
                  //     // width: 36, // 自定义图标的宽度
                  //     // height: 36, // 自定义图标的高度
                  //   ),
                  // ),
                  )
              : const SizedBox();
        },
      );

      if (!_isVisible && _overlayEntry != null) {
        Overlay.of(context).insert(_overlayEntry!);
        _isVisible = true;
        _overlayEntry?.markNeedsBuild();
      }
    });
  }

  static void hideButton() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _isVisible = false;
      _overlayEntry?.markNeedsBuild();
    });
  }

  static void showAgain() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      _isVisible = true;
      _overlayEntry?.markNeedsBuild();
    });
  }
}
